package com.liuqi.business.service.impl;


import com.liuqi.base.BaseMapper;
import com.liuqi.base.BaseServiceImpl;
import com.liuqi.business.constant.ConfigConstant;
import com.liuqi.business.constant.KeyConstant;
import com.liuqi.business.enums.UsingEnum;
import com.liuqi.business.mapper.CurrencyMapper;
import com.liuqi.business.model.ConfigModelDto;
import com.liuqi.business.model.CurrencyModel;
import com.liuqi.business.model.CurrencyModelDto;
import com.liuqi.business.model.LoggerModel;
import com.liuqi.business.service.ConfigService;
import com.liuqi.business.service.CurrencyService;
import com.liuqi.redis.RedisRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
@Transactional(readOnly = true)
public class CurrencyServiceImpl extends BaseServiceImpl<CurrencyModel, CurrencyModelDto> implements CurrencyService {

	@Autowired
	private CurrencyMapper currencyMapper;
	@Autowired
	private RedisRepository redisRepository;
	@Autowired
	private ConfigService configService;
	@Override
	public BaseMapper<CurrencyModel, CurrencyModelDto> getBaseMapper() {
		return this.currencyMapper;
	}

	@Override
	public CurrencyModelDto getById(Long id) {
		CurrencyModelDto config = redisRepository.getModel(KeyConstant.KEY_CURRENCY_ID + id);
		if (config == null) {
			config = currencyMapper.getById(id);
			if (config != null) {
				//缓存一小时
				redisRepository.set(KeyConstant.KEY_CURRENCY_ID + id, config, 2L, TimeUnit.DAYS);
			}
		}
		return config;
	}

	@Override
	public void afterAddOperate(CurrencyModel currencyModel) {
		//记录日志
		LoggerModel log = super.getLogger();
		if (log != null) {
			loggerService.insert(log);
		}
		//TODO 初始化用户钱包
		//userWalletService.initUserWalletByCurrency(currencyModel.getId());
	}

	@Override
	public void afterUpdateOperate(CurrencyModel currencyModel) {
		LoggerModel log = super.getLogger();
		if (log != null) {
			loggerService.insert(log);
		}
	}


	/**
	 * 根据名称获取（缓存）
	 * @param name
	 * @return
	 */
	@Override
	public CurrencyModelDto getByName(String name) {
		String key=KeyConstant.KEY_CURRENCY_NAME+name;
		CurrencyModelDto model = redisRepository.getModel(key);
		if (model == null) {
			model=currencyMapper.getByName(name);
			if(model!=null){
				redisRepository.set(key, model, 2L, TimeUnit.DAYS);
			}
		}
		return model;
	}

	@Override
	public List<CurrencyModelDto> getAll() {
		return this.queryListByDto(new CurrencyModelDto(),false);
	}

	@Override
	public List<CurrencyModelDto> getUsing() {
		CurrencyModelDto search=new CurrencyModelDto();
		search.setStatus(UsingEnum.USING.getCode());
		return this.queryListByDto(search,false);
	}

	@Override
	public void cleanAllCache() {
		List<CurrencyModelDto> list = this.queryListByDto(new CurrencyModelDto(),false);
		if(list!=null && list.size()>0){
			for(CurrencyModel currency:list){
				this.cleanCacheByModel(currency);
			}
		}
	}
	@Override
	public Integer getStatusById(Long id){
		Integer status=0;
		CurrencyModel model=this.getById(id);
		status= model!=null?model.getStatus():0;
		return status;
	}
	@Override
	public Integer getPositionById(Long id) {
		Integer position=0;
		CurrencyModel model=this.getById(id);
		position= model!=null?model.getPosition():0;
		return position;
	}

	@Override
	public String getNameById(Long id) {
		String name="";
		CurrencyModel model=this.getById(id);
		name= model!=null?model.getName():"";
		model=null;
		return name;
	}

	@Override
	public Long getUsdtId() {
		ConfigModelDto model=configService.queryByName(ConfigConstant.CONFIGNAME_USDT);
		return model!=null?Long.valueOf(model.getVal()):0L;
	}

	@Override
	public List<Long> getLikeByName(String currencyName,Integer status) {
		return currencyMapper.getLikeByName(currencyName,status);
	}


	@Override
	public void cleanCacheByModel(CurrencyModel currencyModel) {
		redisRepository.del(KeyConstant.KEY_CURRENCY_ID + currencyModel.getId());
		redisRepository.del(KeyConstant.KEY_CURRENCY_NAME + currencyModel.getName());
		redisRepository.del(KeyConstant.KEY_CURRENCY_PIC_ID + currencyModel.getId());
		redisRepository.del(KeyConstant.KEY_CURRENCY_PIC_NAME + currencyModel.getName());
	}
}
